package auth

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"
	v1 "gitee.com/liuxuezhan/ar-platform/rpc-client/authRoleClient/api/role/v1"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetRolePermissionsLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 获取角色权限列表
func NewGetRolePermissionsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetRolePermissionsLogic {
	return &GetRolePermissionsLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetRolePermissionsLogic) GetRolePermissions(req *types.GetRolePermissionsRequest) (resp *types.GetRolePermissionsResponse, err error) {
	reply, err := l.svcCtx.AuthRoleRpc.GetRolePermissions(l.ctx, &v1.GetRolePermissionsRequest{
		RoleId: req.RoleId,
		Status: v1.Status(req.Status),
	})
	if err != nil {
		return resp, err
	}

	resp = &types.GetRolePermissionsResponse{}
	for _, role := range reply.List {
		resp.List = append(resp.List, types.RolePermissions{
			PermissionId:   role.GetPermissionId(),
			PermissionName: role.GetPermissionName(),
			Status:         int8(role.GetStatus()),
		})
	}

	return resp, err
}
